﻿using Dapper;
using Entity;
using IRepository;
using System;
using System.Collections.Generic;
using Microsoft.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Repository
{
    public class LoginRepository : ILoginRepository
    {
        public bool Login(string username, string pwd, string sqlserver)
        {
            // 关键：将用户输入的明文密码转换为MD5哈希（与数据库存储格式一致）
            string md5Pwd = Md5Helper.ToMd5(pwd);
            Console.WriteLine(md5Pwd);
            using (SqlConnection con = new SqlConnection(sqlserver))
            {
                string sql = "select * from [sys_org_user_management] where user_account = @user_account and user_password = @user_password";
                // 用MD5哈希后的密码去查询数据库
                SysOrgUserManagement user = con.Query<SysOrgUserManagement>(
                    sql,
                    new { user_account = username, user_password = md5Pwd }
                ).FirstOrDefault();

                return user != null; // 存在用户则验证通过
            }
        }
    }
}
